import axios from 'axios';
import { stringify, ParsedUrlQueryInput } from 'querystring';

axios.defaults.baseURL = 'http://floor.huluxia.com';

axios.defaults.timeout = 3500;

axios.defaults.headers = {
    'Content-Type': 'application/x-www-form-urlencoded'
};

axios.interceptors.request.use(function (config) {

    var defaultsQuery = {
        gkey: '000000',
        app_version: '4.0.1.5.4',
        versioncode: '295',
        market_id: 'tool_uc',
        platform:2
    }

    Object.assign(config.params, defaultsQuery);
    console.log(config);
    
    return config;
});

type method = 'post' | 'get' | 'delete' | 'put';

export async function request(path: string, method: method, params: object, data?: object): Promise<object> {

    try {
        var result = await axios({
            url: path,
            method: method,
            params: params,
            data: stringify(data as ParsedUrlQueryInput),
            headers: {
                'User-Agent': 'okhttp/3.8.1'
            }
        })
    } catch (err) {
        // error 处理
        throw {
            err: err
        }
    }

    return {
        data: result.data
    }
}

// re('/user', 'post',{s:'哈哈'},{op:'lala'}).then(doc=>{
//     console.log(doc);

// }).catch(err=>{
//     console.log(err);  
// })
